﻿@{
    ViewBag.Title = "RuleIndex";
    Layout = "~/Views/Shared/_ScheduleClass_View.cshtml";
}

@using ClassModel
@using ClassBLL
@using ClassCommon
@using System.web



@{
    // 原始的taskid
    string rawTaskid = this.Request["taskid"];

    // 根据任务id获取排课节次信息
    int decodeTaskId = ViewBag.decodeTaskid;
    t_class_sections section = t_class_sections_BLL.GetSectionByTaskID(decodeTaskId);

    t_classDemo_UserInfo user = ViewBag.CurrentUser;

    int daysofweek = section.dayOfWeek; // 决定了表格的第一行列
    int amLessionCount = section.AMSections; // 表格的上半部分行
    int pmLessionCount = section.PMSections; // 表格的下半部分行

    int totalSections = (amLessionCount + pmLessionCount) * daysofweek; // 一周上多少节课

    // 
    t_class_fileupload fileupload = t_class_user_fileupalod_BLL.GetExcelFileWithTaskId(decodeTaskId, user.id);

    // 获取老师当前excel的老师数据
    // var teachers = t_class_teacher_BLL.GetTeacherListWithFileId(fileupload.id);
    // 获取课程信息
    var coureses = t_class_course_BLL.GetCourseListWithFileID(fileupload.id);
    // 获取班级的信息
    var classes = t_class_classes_BLL.GetClassListWithFileId(fileupload.id);

    int page = 0;
    int totalCount;
    // 获得老师&课程节点信息
    var bindList = t_class_teacher_BLL.GetTeachersInfoWhitFileId(fileupload.id, out totalCount, page);
    int totalPageCount = 1;
    if (totalCount > 10)
    {
        if (totalCount % 10 == 0)
        {
            totalPageCount = totalCount / 10;
        }
        else
        {
            totalPageCount = totalCount / 10 + 1;
        }
    }
    System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

    string jsonFirstItem = jsSerializer.Serialize(bindList);

    // string firstJsonItem = 

    // 获得年纪信息
    var grade = t_class_grade_BLL.GetGradeInfoWithFileId(fileupload.id);
}

@section headerPartial {
    
}
<div id="divbody" style="min-height: 495px;">
    <div class="container" id="divcon">
        <style>
            a.view-course-plan {
                padding-left: 25px;
                background-repeat: no-repeat;
                margin-left: 10px;
                text-decoration: none;
                padding-bottom: 5px;
            }

            li.pos {
                margin-left: 15px;
            }

            .list-inline a {
                text-decoration: none;
                padding-left: 25px;
                padding-bottom: 5px;
            }
        </style>
        <div class="row" style="margin-top: 20px;">
            <div class="col-md-8">
                <ul class="list-inline">
                    <li>
                        <a href="OperatorClassDetail?id=@ViewBag.Taskid" id="baseInfo" class="task-baseInfo">基础信息</a>
                    </li>
                    <li class="pos">
                        <a href="javascript:void()" id="rule" class="task-rule active task-rule-active">规则条件</a>
                    </li>

                    <li class="pos">
                        <a href="BeginSchedule?taskid=@ViewBag.Taskid" id="auto" class="task-auto">开始排课</a>
                    </li>
                </ul>
            </div>

            <div class="col-md-2 col-md-offset-2">
                <div class="dropdown" style="float: right;">
                    <button id="btnQuestions" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-default dropdown-toggle">
                        排课遇到难题
                           
                        <span class="caret"></span>
                    </button>
                    <ul id="questions-list" class="dropdown-menu dropdown-menu-right" aria-labelledby="btnQuestions">
                        <li>
                            <a href="http://bbs.yunxiao.com/forum.php?mod=viewthread&amp;tid=3738&amp;extra=page%3D1" target="_blank">任课老师变化之后规则条件要重新设置吗？</a>
                        </li>
                        <li>
                            <a href="http://bbs.yunxiao.com/forum.php?mod=viewthread&amp;tid=3729&amp;extra=page%3D1" target="_blank">希望任课教师的课时分布平均一些，如何设置？</a>
                        </li>
                        <li>
                            <a href="http://bbs.yunxiao.com/forum.php?mod=viewthread&amp;tid=3731&amp;extra=page%3D1" target="_blank">规则条件里的“其它”可以自己添加吗？</a>
                        </li>
                        <li>
                            <a href="http://bbs.yunxiao.com/forum.php?mod=viewthread&amp;tid=3730&amp;extra=page%3D1" target="_blank">什么时候要用到“合班”？</a>
                        </li>
                        <li>
                            <a href="http://bbs.yunxiao.com/forum.php?mod=viewthread&amp;tid=3737&amp;extra=page%3D1%26filter%3Dtypeid%26typeid%3D9"
                                target="_blank">不排课时间较多的时候，操作太麻烦怎么办？</a>
                        </li>
                        <li>
                            <a href="http://bbs.yunxiao.com/forum.php?mod=viewthread&amp;tid=3347&amp;fromuid=26" target="_blank">“添加标签”有什么作用？</a>
                        </li>
                        <li>
                            <a href="http://bbs.yunxiao.com/forum.php?mod=viewthread&amp;tid=3728&amp;extra=page%3D1" target="_blank">不排课时间怎么设置？</a>
                        </li>
                    </ul>
                </div>
            </div>



        </div>
        <script src="/Static/Template/js/course-plan.js" charset="utf-8"></script>
        <script type="text/javascript">
            var coursePlanService = new CoursePlanService();
            var hasCoursePlan = 0;
            $(function () {
                initButton();
            })
            function initButton() {
                coursePlanService.hasCoursePlan(taskId, function (has) {
                    hasCoursePlan = has;
                    $("li[show-by-courseplan='1']").hide();
                    if (has == "1") {
                        $("li[show-by-courseplan='1']").show();
                    }
                })
            }
        </script>
   
        <div class="row">
            <div class="col-md-8" style="padding-top: 10px">
                请设置学校教学时间、年级/班级固定无课时间、课程/教师固定无课时间以及连堂课和合班课
            </div>
        </div>
        <div style="margin-top: 10px; border-top: 1px solid #0599f7"></div>
        <style type="text/css">
            .my-list-inline {
                padding-left: 0;
                list-style: none;
                border-bottom: 1px solid #b3b3b3;
                display: inline-block;
            }

                .my-list-inline > li {
                    display: inline-block;
                    width: 104px;
                    text-align: center;
                    margin-left: -4px;
                    cursor: pointer;
                }

                    .my-list-inline > li:first-child {
                        margin-left: 0px;
                    }

                .my-list-inline .active {
                    color: #ec5d41;
                    border-bottom: 4px solid #ec5d41;
                    cursor: default;
                }

                .my-list-inline a {
                    color: #666666;
                    font-size: 13px;
                    text-decoration: none;
                }

            a.colorful {
                color: #ec5d41;
            }

            a.view-unSchedule-rule {
                text-decoration: none;
            }
        </style>
        <!-- 横向菜单节点 -->
        <div class="row" style="margin-top: 20px;">
            <div class="col-md-8">
                <ul class="my-list-inline">
                    <li id="task-rule-unScheduleTime" class="active">
                        <a href="javascript:void(0);" class="colorful">不排课时间</a>
                    </li>
                    <li id="task-rule-mergedClasses">
                        <a href="MergeClassConfig?taskId=@(rawTaskid)">合班</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="row" style="margin-top: 20px;">
            <div class="col-md-8">
                <div class="ruleSltDiv" style="float: left; line-height: 34px;">
                    <select id="unScheduleRuleTypeSlt" class="form-control" onchange="setRuleType()">
                        <option value="class">班级不排课时间</option>
                        <option value="course">课程不排课时间</option>
                        <option value="teacher">教师不排课时间</option>
                    </select>
                </div>
            </div>
            <div class="col-md-4 saveBtnDiv" style="text-align: right; line-height: 34px;">
                <button id="saveBtn" type="button" class="btn btn-primary btn-small" style="height: 32px;" onclick="getGenerateResult()">
                    保存
                   
                </button>
            </div>
        </div>
        <ul id="tagArea" class="list-unstyled list-inline" style="margin-top: 20px">
        </ul>
        <!-- 后台绑定数据的HTML节点 -->
        <div style="margin-top: 10px;">
            <div class="row">
                <div class="col-md-3 leftNav">
                    <!-- 班级节点 -->
                    <div id="mainNav_class" class="tree">
                        <ul>
                            <i class="glyphicon glyphicon-minus-sign"></i><span>@(grade.gradename)</span>
                            @if (classes != null)
                            {
                                for (int c = 0; c < classes.Count; c++)
                                {
                                <li style="display: list-item;" data-cid="@(classes[c].id)"><span>@(classes[c].classname)</span></li>
                                }
                            }


                        </ul>
                    </div>
                    <!-- 课程节点 -->
                    <div id="mainNav_course" class="tree" style="display: none;">
                        @if (coureses != null)
                        {
                            for (int cos = 0, count = coureses.Count; cos < count; cos++)
                            {
                            <ul data-cid="@(coureses[cos].id)">
                                <i class="glyphicon glyphicon-plus-sign"></i><span class="">@(coureses[cos].coursename)</span><li style="display: none;"><span>@(grade.gradename)</span></li>
                            </ul>
                            }
                        }

                    </div>

                    <!-- 老师节点 -->
                    <div id="mainNav_teacher" class="tree" style="display: none">
                        <!-- 搜索老师 -->
                        @*  <div id="teacherNameInputDiv">
                            <input type="text" id="teacherNameInput" onkeydown="searchTeacherByEnterKey()" class="txtSearch">
                            <button type="button" class="btn btn-info btn-sm btnSearch" onclick="searchTeacher()">
                                <span class="glyphicon glyphicon-search"></span>&nbsp;查询
                               
                            </button>
                        </div>*@
                        <table class="teacher teacherhover">
                            <thead>
                                <tr>
                                    <th width="40%">教师</th>
                                    <th>课程</th>
                                </tr>
                            </thead>

                            <!-- 老师节点 -->
                            <input type="hidden"  id="pageTeacher" value="0" data-currentPage="0" data-totalCount="@(totalCount)" data-taskid="@(rawTaskid)"/>
                            <tbody id="tcbody">
                                @if (bindList != null)
                                {
                                    for (int bb = 0, count = bindList.Count; bb < count; bb++)
                                    {
                                    <tr data-tid="@(bindList[bb].tid)" data-cid="@(bindList[bb].cid)" data-classesCount="@(bindList[bb].classesCount)" onclick="javascript:void(0);">
                                        <td>@(bindList[bb].tname)</td>
                                        <td>@(bindList[bb].cname)</td>
                                    </tr>
                                    }
                                }

                            </tbody>

                        </table>
                        <!-- 翻页 -->
                        <div class="pager" id="pageUl_teacher">
                            <a href="javascript:my_gotoPage_teacher(false)" class="glyphicon glyphicon-step-backward" title="上一页" style="margin-right: 10px;"></a>
                            <label id="currentPage_teacher">1</label>/
                               
                            <label id="totalPage_teacher">@(totalPageCount)</label>
                            <a href="javascript:my_gotoPage_teacher(true)" class="glyphicon glyphicon-step-forward" title="下一页" style="margin-left: 10px;"></a>
                        </div>
                    </div>
                </div>
                <div class="col-md-9">
                    <!-- 课表table -->
                    <table class="mainTable">
                        <thead>
                            <tr id="rightTable_head">
                                <td width="8%">序号</td>

                                @for (int dow = 0; dow < daysofweek; dow++)
                                {
                                    <td width="18.4%">
                                        <div class="float-left table-header-day-of-week">@(DataFormatHelper.WeekStrWithNum(dow))</div>
                                        @* <div class="float-left">
                                            <div class="table-header-item">
                                                <label>
                                                    <input type="checkbox" data-id="@(dow)_0">上午不排</label>
                                            </div>
                                            <div class="table-header-item">
                                                <label>
                                                    <input type="checkbox" data-id="@(dow)_1">下午不排</label>
                                            </div>
                                        </div>*@
                                    </td>
                                }

                            </tr>
                        </thead>
                        <tbody id="rightTable_body">
                            @{
                                int allrows = amLessionCount + pmLessionCount;
                                string css = string.Empty;
                                for (int all = 0; all < allrows; all++)
                                {
                                    // 设置分隔符样式
                                    if (all == amLessionCount - 1) { css = "border-bottom: 2px solid #ccc"; } else { css = string.Empty; }
                                <tr style="@(css)">

                                    <td>@(all + 1)</td>
                                    @for (int cols = 0; cols < daysofweek; cols++)
                                    {
                                        <td class="timetable_avail_td" data-isPK="1" data-id="@(all + 1)_@(cols + 1)">&nbsp;</td>
                                    }

                                </tr>
                                }
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <!--浮层-提示框-->
        <div id="alertDiv" style="position: absolute; left: 50%; top: 50%; padding: 20px; width: 300px; display: none">
        </div>
        <!-- 添加标签 Modal -->
        <div class="modal fade modal-ruleList" id="addTagsModal" tabindex="-1" role="dialog" aria-labelledby="addTagsModalLabel"
            aria-hidden="false">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">×</span>
                        </button>
                        <div class="modal-title">
                            <b>添加标签</b>
                        </div>
                        <div style="margin-top: 10px;">
                            <span class="text-info">* 标签名请尽量简短，不能是数字、特殊符号或者标点符号</span>
                        </div>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label for="txtName" class="col-sm-2 control-label">
                                    <span class="text-info">* 标签名</span>
                                </label>

                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="tagName">
                                </div>
                            </div>
                        </form>
                        <div style="margin-top: 10px;">
                            <label id="addTagResult"></label>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" onclick="tagSubmit()">保存</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- updateTagsModal -->
        <div class="modal fade modal-ruleList" id="updateTagsModal" tabindex="-1" role="dialog" backdrop="static" aria-labelledby="addTagsModalLabel"
            aria-hidden="false">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">×</span>
                        </button>
                        <div class="modal-title">
                            <b>修改标签</b>
                        </div>
                        <div style="margin-top: 10px;">
                            <span class="text-info">* 标签名请尽量简短，不能是数字、特殊符号或者标点符号</span>
                        </div>
                    </div>
                    <div class="modal-body">
                        <table id="updateTagInfos" class="table table-bordered text-center">
                            <thead>
                                <tr>
                                    <td>标签名</td>
                                    <td>操作</td>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                        <hr>
                        <div style="margin-top: 10px;">
                            <label id="updateTagResult" class="text-red"></label>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>

        <!--预排冲突列表 Modal -->
        <div class="modal fade modal-ruleList" id="preListModal" tabindex="-1" role="dialog" aria-labelledby="ruleListModalLabel"
            aria-hidden="false">
            <div class="modal-dialog list-modal">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">×</span>
                        </button>
                        <div class="modal-title" style="margin-top: 40px">
                            <b>此节课时已经预排的课程 : </b>
                            <span id="preListModalTitle"></span>
                        </div>
                    </div>
                    <div class="modal-body">
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <td>年级</td>
                                    <td>班级</td>
                                    <td>老师</td>
                                    <td>科目</td>
                                </tr>
                            </thead>
                            <tbody id="preListModalTable">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!--不排课规则冲突列表 Modal -->
        <div class="modal fade modal-ruleList" id="subClassListModal" tabindex="-1" role="dialog" aria-labelledby="ruleListModalLabel"
            aria-hidden="false">
            <div class="modal-dialog list-modal">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">×</span>
                        </button>
                        <div class="modal-title" style="margin-top: 40px">
                            <b>此节课时已经设置不排课的班级 : </b>
                            <span id="subClassListModalTitle"></span>
                        </div>
                    </div>
                    <div class="modal-body">
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <td>年级</td>
                                    <td>班级</td>
                                    <td>不排课类型</td>
                                </tr>
                            </thead>
                            <tbody id="subClassListModalTable">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <link href="/static/template/css/ruleDialog.css" rel="stylesheet">
        <style type="text/css">
            #ruleListTable_body td {
                padding: 8px;
                font-size: 14.8px;
                text-align: left;
            }
        </style>
        <!--规则列表 Modal -->
        <div class="modal fade modal-ruleList" id="ruleListModal" tabindex="-1" role="dialog" aria-labelledby="ruleListModalLabel"
            aria-hidden="false">
            <div class="modal-dialog" style="width: 900px">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">×</span>
                        </button>
                        <div class="modal-title" id="ruleListModalLabel">
                            <b>规则列表</b>
                        </div>
                        <div style="margin-top: 10px;">
                            <select id="unScheduleRuleListTypeSlt" onchange="ruleViewer.getRuleList()" class="form-control" style="margin-bottom: 10px; width: 200px;">
                                <option value="all" selected="">所有规则</option>
                                <option value="unschedule_grade">年级不排课</option>
                                <option value="unschedule_class">班级不排课</option>
                                <option value="unschedule_course">课程不排课</option>
                                <option value="unschedule_course_grade">年级课程不排课</option>
                                <option value="unschedule_teacher">教师不排课</option>
                                <option value="preschedule">预排</option>
                                <option value="importance_japq">教案平齐</option>
                                <option value="importance_zrkfb">周任课分布</option>
                                <option value="importance_rrkfb">日任课分布</option>
                                <option value="importance_lsxz">连上限制</option>
                            </select>
                            <span id="ruleStatistics"></span>
                        </div>
                    </div>
                    <div class="modal-body">
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <td width="130">规则</td>
                                    <td>细则</td>
                                    <td style="width: 100px">重要性</td>
                                    <td width="50">操作</td>
                                </tr>
                            </thead>
                            <tbody id="ruleListTable_body">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <script src="/Static/Template/js/rule-statistic.js" charset="UTF-8"></script>
        <script src="/Static/Template/js/rule-view.js" charset="UTF-8"></script>
        <script type="text/javascript">
            var _hmt = _hmt || [];
            (function () {
                var hm = document.createElement("script");
                hm.src = "https://hm.baidu.com/hm.js?7c66c07d4e8cc54deba8d7f967efe75b";
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(hm, s);
            })();
        </script>
        <script type="text/ecmascript">
            var ruleViewer = new RuleViewer();
        </script>
        <script type="text/ecmascript">
            var isMyTask = true
        </script>
        <script type="text/javascript">
            ruleViewer.setTaskId('2640188197994496')
        </script>

        <script type="text/javascript" src="/Static/Template/js/common(1).js"></script>
        <script type="text/javascript" src="/Static/Template/js/ruleUtil.js"></script>
        <script type="text/javascript" src="/Static/Template/js/unScheduleRule.js"></script>
        <script type="text/javascript" src="/Static/Template/js/classUnSchedule.js"></script>
        <script type="text/javascript" src="/Static/Template/js/courseUnSchedule.js"></script>
        <script type="text/javascript" src="/Static/Template/js/teacherUnSchedule.js"></script>
        <script type="text/javascript" src="/Static/Template/js/tagService.js"></script>


        <script type="text/javascript">
            var basePath = "";
            var unScheduleRuleType = "class";//默认不排课规则类型
            var taskId = "2640188197994496";//排课任务
            var selectedNodeValue = "";//选中的节点id
            var selectedNodeType = "";//选中的节点类型
            var selectedTagId = ""; //选中的tagId
            var selectedTagName = ""; //选中的tagName
        </script>

        <link href="/static/template/css/unSheduleRule.css" rel="stylesheet">
        <link href="/static/template/css/step-box.css" rel="stylesheet">
        <script type="text/javascript" src="/static/template/js/step-box.js"></script>

        <script type="text/javascript">
            stepBox.init();
            $(function () {
                $(".dropdown-toggle").dropdown();
                getContentList("qRule");
            })
        </script>

        <script>
            var isMyTask = true
        </script>


        <!--修改课时设置-->
    </div>
</div>
<input type="hidden" id="totalSections" value="@(totalSections)"/>


@section scripts {

    <script type="text/javascript">

        var totalSections = parseInt($("#totalSections").val());

        /// 从当期数组中，找到对应的节点对象
        Array.prototype.findNode = function (type, dataid) {
            for (var i = 0; i < this.length; i++) {
                if (this[i].type == type && this[i].dataId == dataid) {
                    return this[i];
                }

            }

            // 否则创建节点，并返回.
            var node = new NodeConstrutor(type, dataid);
            this.push(node);
            return node;
        }

        // 判断元素节点在数组中是否存在
        Array.prototype.in_array = function (nodeData) {
            for (var i = 0; i < this.length ; i++) {
                if (this[i] === nodeData) {
                    return true;
                }
            } 

            return false;
        }


        var classSelectNode = []; //班级选择节点
        var couresSelectNode = []; // 课程选择节点
        var teacherSelectNode = []; // 老师选择节点

        // 首先加载用户已经存好的不排课数据
        (function loadNoScheduleData() {
            var taskId = $("#pageTeacher").attr("data-taskid");
            $.get("getNoScheduleList",
                {
                    taskid: taskId
                },
                function (data) {
                    // class不排课
                    if (data.classList.length > 0) {

                        console.log(data.classList instanceof Array);

                        data.classList.forEach(function (item, index) {
                            var clsNode = new NodeConstrutor("class", item.dataid);
                            item.data.split(',').forEach(function (item2, index2) {
                                clsNode.nodes.push(item2);
                            })

                            classSelectNode.push(clsNode);
                        })
                    }

                    // teacher不排课
                    if (data.teacherList.length > 0) {
                        data.teacherList.forEach(function (teacher, index) {
                            var teaNode = new NodeConstrutor("teacher", teacher.dataid);
                            teacher.data.split(",").forEach(function (datas, index) {
                                teaNode.nodes.push(datas);
                            })

                            teacherSelectNode.push(teaNode);
                        })
                    }

                    // course不排课
                    if (data.courseList.length > 0) {
                        data.courseList.forEach(function (course, index) {
                            var courseNode = new NodeConstrutor("course", course.dataid);
                            course.data.split(",").forEach(function (datas, index) {
                                courseNode.nodes.push(datas);
                            });

                            couresSelectNode.push(courseNode);
                        });
                    }
                })
        })()

        function checkHasSelectNode() {
            if (couresSelectNode.length == 0 && teacherSelectNode.length == 0 && classSelectNode.length == 0) {
                alert("请在左侧，选择你需要操作的节点");
                return false; // 取消checkbox的默认选中/取消行为。
            }

            return true;
        }

        var teacherClassArray = [];
        // 缓存第老师&课程第一页的数据
        teacherClassArray.push(@(Html.Raw(jsonFirstItem)));

        // 老师翻页
        function my_gotoPage_teacher(direction) {
            var $dataInput = $("#pageTeacher");
            var currentPage = parseInt($dataInput.attr("data-currentPage")); // 当前页码
            var totalPageCount = parseInt($dataInput.attr("data-totalCount")); // 总页数
            var rawTaskId = $dataInput.attr("data-taskid"); // 任务id

            var maxPageCount = totalPageCount % 10 == 0 ? totalPageCount / 10 - 1 : totalPageCount / 10;
            maxPageCount = parseInt(maxPageCount);
            if (direction) { // 下一页
                currentPage = currentPage >= maxPageCount ? maxPageCount : ++currentPage
            } else { // 上一页
                currentPage == 0 ? 0 : --currentPage;
            }
            $("#currentPage_teacher").text(currentPage + 1);
            $dataInput.attr("data-currentPage", currentPage.toString());

            if (typeof teacherClassArray[currentPage] === "object") {
                //console.log("本地缓存了");
                builderHTMLWithIndex(currentPage);
            } else {
                $.get("TeacherAndClassApi", {
                    taskid: rawTaskId,
                    pageIndex: currentPage
                }, function (data) {
                    if (data.status) {
                        teacherClassArray.push(data.list);
                        builderHTMLWithIndex(currentPage);
                    } else {
                        alert(data.info);
                    }
                })
            }
        }

        /// 翻页后的老师&课程数据绑定
        function builderHTMLWithIndex(index) {
            var $tcbody = $("#tcbody");
            var arrayItem = teacherClassArray[index];
            var trStr = "";
            for (var a = 0; a < arrayItem.length; a++) {
                // <tr data-tid="141" data-cid="36" onclick="javascript:void(0);">
                trStr += "<tr data-tid='" + arrayItem[a].tid + "' data-cid='" + arrayItem[a].cid + "' data-classesCount='"+arrayItem[a].classesCount+"'><td>" + arrayItem[a].tname + "</td><td>" + arrayItem[a].cname + "</td></tr>";
            }

            $tcbody.html("");
            $tcbody.html(trStr);

            bindTeacherClsTrigger();
        }

        ///不排课
        (function () {
            $(".mainTable tbody td").on("click", function () {
                // 用户当前操作的面板
                var indexTabStr = $("#unScheduleRuleTypeSlt").find("option:selected").val();

                var operatorNodeArray; // 操作的数组
                var operatorNodeHTML;// 操作的 HTML 结构
                var operatiorNodeIndex;// 操作的索引
                var operatiorNodeType; // 操作的节点类型。

                if (indexTabStr == "class") { // 班级面板
                    // 还原用户选择的班级不排课数据

                    // 获取自面板索引
                    var classLis = $("#mainNav_class ul li");
                    var clsINDEX = -1;
                    for (var i = 0; i < classLis.length; i++) {
                        if ($(classLis[i]).hasClass("active_item")) {
                            clsINDEX = i;
                            break;
                        }
                    }

                    if (clsINDEX == -1) {
                        alert("请选择你需要操作的班级数据!");
                        return;
                    }

                    operatorNodeArray = classSelectNode;
                    operatorNodeHTML = classLis;
                    operatiorNodeIndex = clsINDEX;
                    operatiorNodeType = "class";

                } else if (indexTabStr == "course") { // 课程面板
                    // 还原用户选择的课程不排课数据

                    // 获取课程面板操作的课程索引
                    var selectULIndex = -1;
                    var uls = $("#mainNav_course ul");
                    for (var i = 0; i < uls.length; i++) {
                        var $ul = $(uls[i]);
                        if ($ul.find("span").hasClass("active_item")) {
                            selectULIndex = i;
                            console.log("selectIndex = " + i);
                            break;
                        }
                    }
                    if (selectULIndex == -1) {
                        alert("请选择你需要操作的课程数据!");
                        return;
                    }
                    // alert($("#mainNav_course ul").eq(selectULIndex).find("span[display!=none]").text());
                    operatorNodeArray = couresSelectNode;
                    operatorNodeHTML = uls;
                    operatiorNodeIndex = selectULIndex;
                    operatiorNodeType = "course";
                } else { // 老师面板
                    // 还原用户选择的老师不排课数据
                    var selectTeacherIndex = -1;
                    var trs = $("#mainNav_teacher tbody tr");
                    for (var t = 0; t < trs.length; t++) {
                        var $tr = $(trs[t]);
                        if ($tr.hasClass("active_item")) {
                            selectTeacherIndex = t;
                            break;
                        }
                    }

                    if (selectTeacherIndex == -1) {
                        alert("请选择你需要操作的老师数据!");
                        return;
                    }

                    operatorNodeArray = teacherSelectNode;
                    operatorNodeHTML = trs;
                    operatiorNodeIndex = selectTeacherIndex;
                    operatiorNodeType = "teacher";

                    // 2018年04月15日任务。
                    // 把课程、老师、班级的数据记录到对应的数组中去。
                    // 有2个关键点
                    // 1. 不要重复记录数据。
                    // 2. 值需要记录不排课的数据，排课的数组不需要记录。(td 排课里面已经包含了位置信息，可以推算出来哪些天的哪节课是不排课的。)
                    // ------------------------------------------------------------------------------------------------------
                    // 3. 每次切换大 tab，切换小 tab 的时候，都需要将数据还原到表格上。 也就说说，每一个菜单都对应一个“独立”的 table 表格。
                    // 4. 最后整理这些记录好的数据，回传到后台，存储到数据库。
                    // 5. 当前端数据整理，和后端数据回传存储完成之后。 需要在界面加载的时候，首先从数据库加载这些数据，绑定到界面，并且根据这些数据去刷新每个菜单对应的“独立”的表格。

                }

                var $this = $(this);
                if ($this.index() == 0) return;
                $this.text($this.text() == "不排课" ? "" : "不排课");
                if ($this.text() == "不排课") {
                    // 设置成不排课为真标记
                    $this.attr("data-isPK", "0");
                } else {
                    $this.attr("data-isPK", "1");
                }

                var position = $this.attr("data-id");
                var ispk = $this.attr("data-ispk");
                // 存储格式 1_1_0 = 星期一_第一节课_不排课
                var nodeData = position + "_" + ispk;
                // alert("选中的单元格是 : " + position + " 是否排课 ：" + (ispk == 1 ? "排课" : "不排课"));

                // 获取选中的老师、课程、班级操作的则侧菜单 
                // li -> class ul - course tr - teacher
                var selectedChildMenu = operatorNodeHTML.eq(operatiorNodeIndex);
                if (operatiorNodeType == "class") { // 操作的是班级节点
                    // 获取班级信息
                    var cid = selectedChildMenu.attr("data-cid");
                    var cname = selectedChildMenu.find("span").text();
                    console.log("当前选中的班级 id : " + cid + "   当前的班级名称 : " + cname);

                    var node = operatorNodeArray.findNode("class", cid);

                    if (ispk == "0") { // 不排课
                        if (!node.nodes.in_array(nodeData)) {
                            node.nodes.push(nodeData);
                        }
                    } else { // 排课
                        node.nodes.pop(nodeData);
                    }

                    console.log(node.type, node.dataId, node.nodes);

                } else if (operatiorNodeType == "course") { // 操作的是课程节点
                    var coursename = selectedChildMenu.find(">span").text();
                    var courseid = selectedChildMenu.attr("data-cid");

                    console.log("当前选中的课程名称 :" + coursename + " 当前课程的 id 是 : " + courseid);

                    var node = operatorNodeArray.findNode("course", courseid);
                    if (ispk == "0") { // 不排课
                        if (!node.nodes.in_array(nodeData)) {
                            node.nodes.push(nodeData);
                        }
                    } else {
                        node.nodes.pop(nodeData);
                    }

                } else { // 操作的是老师节点

                    // 老师的姓名
                    var teachername = selectedChildMenu.find("td").eq(0).text();
                    // 老师教的课程名
                    var coursename = selectedChildMenu.find("td").eq(1).text();
                    // 老师的 id
                    var teacherid = selectedChildMenu.attr("data-tid");
                    // 课程的 id
                    var courseid = selectedChildMenu.attr("data-cid");
                    // 此老师的上课节次数
                    var classesCount = parseInt(selectedChildMenu.attr("data-classesCount"));

                    console.log("老师的姓名 : " + teachername + " teacherid : " + teacherid + " coursename : " + coursename + " courseid : " + courseid);

                    var node = operatorNodeArray.findNode("teacher", teacherid);
                    if (ispk == "0") { // 不排课
                        if (node.nodes.length + classesCount >= totalSections) {
                            alert("此老师一周至少上 [" + classesCount + "] 节课!");
                            $this.attr("data-isPK", "0");
                            $this.text("");
                            return;
                        }
                        if (!node.nodes.in_array(nodeData)) {
                            node.nodes.push(nodeData);
                        }
                    } else {
                        node.nodes.pop(nodeData);
                    }

                    console.log(node.nodes.length);
                }

            });
        })();

        //课程样式切换
        $("#mainNav_course ul").on("click", function () {
            var $ul = $(this);
            if ($ul.index() == 0) { $ul.find("span").removeClass("selected_node"); }
            $ul.find("span").addClass("active_item");
            $ul.siblings().find("span").removeClass("active_item");

            // 班级id
            var cosId = $ul.attr("data-cid");
            // var cosType = "courese";// 课程表示类型
            // 当前节点的课程
            var courseData = couresSelectNode.findNode("course", cosId);
            refreshTable(courseData);

        });

        // 班级样式切换
        $("#mainNav_class li").on("click", function () {
            var $li = $(this);
            $li.addClass("active_item");
            $li.siblings().removeClass("active_item");
            //var clsType = "class"; //标记标志类型
            var clsId = $li.attr("data-cid");
            // 当前班级节点的数据
            var clsData = classSelectNode.findNode("class", clsId);
            refreshTable(clsData);

        });


        function bindTeacherClsTrigger() {
            // 老师样式切换
            $("#tcbody tr").on("click", function () {
                var $tr = $(this);
                $tr.addClass("active_item");
                $tr.siblings().removeClass("active_item");

                var tid = $tr.attr("data-tid"); // 老师id
                //var cid = $tr.attr("data-cid");// 老师教的课程id
                //var ttype = "teacher";// 老师标志类型
                //var ttid = tid + "|" + cid; // 当前老师数据标识
                // 当前老师节点的数据
                var teacherData = teacherSelectNode.findNode("teacher", tid);
                refreshTable(teacherData);
            });
        }

        bindTeacherClsTrigger();

        /// 根据每个节点已经存储的数据，刷新整个表格
        function refreshTable(data) {

            // 清空整张表格为都排课
            // .timetable_avail_td
            var $tds = $("#rightTable_body td.timetable_avail_td");
            // $(selector).each(function(index,element))
            $tds.each(function (index, item) {
                $(item).attr("data-ispk", "1"); // 设置成全部排课
                $(item).text(""); // 清空所有的不排课文字。

            })
            //$("#rightTable_body td.timetable_avail_td").attr("data-ispk", "1");
            //$("#rightTable_body td.timetable_avail_td").text("");

            // 然后根据节点数据来重新刷新表格哪些cell需要显示不排课
            for (var i = 0; i < data.nodes.length; i++) {
                //1_1_0
                var nodeStr = data.nodes[i];
                var nodePrefix = nodeStr.substring(0, 3);

                $tds.each(function (index, item) {
                    var dataId = $(item).attr("data-id");
                    if (dataId == nodePrefix) {
                        $(item).attr("data-ispk", "1");
                        $(item).text("不排课");
                    }
                });
            }
        }

        function ampmBind() {
            $(".mainTable thead input[type=checkbox]").on("click", function () {

                if (!checkHasSelectNode()) return false;

                var $this = $(this);
                var data = $(this).attr("data-id");
                var dataArr = data.split('_');
                var dayOfWeek = dataArr[0]; // 0-4 0 表示星期一  4 表示星期五
                var amOrPm = dataArr[1]; // 0 上午 1 下午

            });
        }

        ampmBind();


        /// 创建节点保存对象
        function NodeConstrutor(type, dataId) {
            //if (typeof this === "window") {
            //    return new NodeConstrutor(type,dataId);
            //}

            this.type = type; // 保存节点类型. 老师、课程、班级。
            this.dataId = dataId; // 保存是哪个老师、哪个班级、哪个课程。
            this.nodes = []; // 用户选择的不排序数据。
        }

        var node = new NodeConstrutor(type, dataid) // 创建节点对象

        // 判断数组中是否包含此对象。
        Array.prototype.contains = function (type, dataid) {

            if (this.length == 0) return false;

            var isContains = false;
            for (var i = 0; i < this.length; i++) {
                if (this[i].type == type && this[i].dataId == dataid) {
                    isContains = true;
                    break;
                }
            }

            return isContains;
        }

       


        function getGenerateResult() {
            // 用户不排课设置数据提交
            // alert("提价用户设置的数据!");
            // 用户的数据
            /*
                1. 班级选择
                    1.1 每个班级id
                    1.2 每个班级的nodes节点数据
                
                2. 课程选择
                    2.1 每个课程id
                    2.2 每个课程的nodes节点数据

                3. 老师选择
                    3.1 每个老师的id
                    3.2 每个老师的nodes节点数据
            */

            var classPostData = getPostDataWithSelectNode(classSelectNode);
            var coursePostData = getPostDataWithSelectNode(couresSelectNode);
            var teacherPostData = getPostDataWithSelectNode(teacherSelectNode);

            console.log("classPostData : " + classPostData);
            console.log("coursePostData : " + coursePostData);
            console.log("teacherPostData : " + teacherPostData);

            /**
              string taskid, // 任务id
            string classPostData, // 班级不排课数据
            string teacherPostData, // 老师不排课数据
            string coursePostData // 课程不排课数据
            */

            $.ajax({
                type: "POST",
                url: "getScheduleData",
                data: {
                    taskid: $("#pageTeacher").attr("data-taskid"),
                    classPostData: classPostData,
                    teacherPostData: teacherPostData,
                    coursePostData: coursePostData
                },
                dataType: "json",
                success: function (data) {
                    console.log(data.status);
                    console.log(data.info);
                    alert(data.info);
                }

            });

        }

        /// 根据用户传入的班级、老师、课程节点，返回需要post到服务器的数据。
        function getPostDataWithSelectNode(selectNode) {
            var postData = "";
            if (selectNode.length <= 0) return postData;
            for (var s = 0; s < selectNode.length; s++) {
                // 获取节点
                var node = selectNode[s];
                if (node.nodes.length > 0) {
                    var dataid = node.dataId;
                    postData += dataid + "|";
                    for (var nn = 0; nn < node.nodes.length; nn++) {
                        postData += node.nodes[nn] + ",";
                    }

                    postData = postData.substring(0, postData.length - 1) + "&";
                }
            }

            return postData;
        }


    </script>

    <style type="text/css">
        .active_item {
            background-color: #37ABF1 !important;
        }
    </style>
}
